using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Extensions.Configuration.Database.Sample
{
    public class ConfigurationBackgroundService : BackgroundService
    {
        private readonly ILogger<ConfigurationBackgroundService> _logger;
        private readonly IConfiguration _configuration;

        public ConfigurationBackgroundService(ILogger<ConfigurationBackgroundService> logger,
            IConfiguration configuration)
        {
            _logger = logger;
            _configuration = configuration;
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                foreach (var item in this._configuration.AsEnumerable())
                {
                    this._logger.LogInformation($"{item.Key}=>{item.Value}");
                }

                //https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/configuration/?view=aspnetcore-5.0
                await Task.Delay(5000, stoppingToken);
            }
        }
    }
}